<?php

namespace App\Http\Controllers\Admin\Rebate;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Rebate\RebateRepository;
use Illuminate\Http\Request;

// 主播
class IndexController extends BaseController
{
    private $repoist;

    public function __construct(RebateRepository $repository)
    {
        $this->repoist = $repository;
    }

    public function index(Request $request)
    {
        $where = [];
        if ($request->has('name') && !empty($request->get('name'))) {
            $where['where']['name'] = $request->get('name');
        }
        if (($request->has('status')) && ($request->get('status') != '') && ($request->get('status') != -1)) {
            $where['where']['status'] = (int)$request->get('status');
        }
//        print_r($request);
        if ($request->has('dateRange') && !empty($request->get('dateRange'))) {
            $time = $request->get('dateRange');
            $where['between'] = ['created_at' => [$time]];
        }
//         var_dump($where);
        return $this->repoist->index($where, $request->get('pageNum'), $request->get('pageSize'));
    }

//    订单返费明细
//    public function details(Request $request)
//    {
//        $where = [];
//        if ($request->has('is_online') && trim($request->get('is_online')) !== '') {
//            $where['where']['is_online'] = (int)$request->get('is_online');
//        }
//        if (($request->has('rebate_id')) && !empty($request->get('rebate_id'))) {
//            $where['where']['rebate_id'] = (int)$request->get('rebate_id');
//        }
//        if (($request->has('product_id')) && !empty($request->get('product_id'))) {
//            $where['where']['product_id'] = (int)$request->get('product_id');
//        }
//        if (($request->has('rebate_status')) && trim($request->get('rebate_status')) !== '') {
//            $where['where']['rebate_status'] = (int)$request->get('rebate_status');
//        }
//        if ($request->has('dateRange') && !empty($request->get('dateRange'))) {
//            $time = $request->get('dateRange');
//            $where['between'] = ['created_at' => [$time]];
//        }
////        首次加载其它的信息
//        $loadExtra = 0;
//        if ($request->has('loadExtra')) {
//            $loadExtra = (int)$request->get('loadExtra');
//        }
//        return $this->repoist->details($where, $request->get('pageNum'), $request->get('pageSize'), $loadExtra);
//    }

//    修改订单的卡的在网状态
    public function changeIsOnline(Request $request)
    {
        $id = $request->get("id");
        $value = (int)$request->get('status');
        $userId = $request->user->id;
        return $this->repoist->changeIsOnline($userId, $id, $value);
    }

//修改卡的余额
    public function changeCardBalance(Request $request)
    {
        $id = $request->get("id");
        $value = (float)$request->get('money');
        $userId = $request->user->id;
        return $this->repoist->changeCardBalance($userId, $id, $value);
    }

//审核订单
    public function reviewIt(Request $request)
    {
        $id = $request->get("id");
        $status = (int)$request->get('status');
        $reason = trim($request->get('reason'));
        $userId = $request->user->id;
        return $this->repoist->reviewIt($userId, $id, $status, $reason);
    }

//返回审核不通过的原因
    public function reviewReasonList(Request $request)
    {
        $id = $request->get("id");
//        $userId = $request->user->id;
        return $this->repoist->reviewReasonList($id);
    }

//日志列表
    public function logsList(Request $request)
    {
        $id = $request->get("id");
        return $this->repoist->logsList($id);
    }

    public function add(Request $request)
    {
//        不能用通用的增加，要重写
        return $this->repoist->add($request);
    }

    public function edit(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
        return $this->repoist->edit($request);
    }

//    public function del(Request $request, $id)
//    {
//        if (!$id) {
//            return $this->returnMsg(1, '请传递参数');
//        }
//        return $this->repoist->unifiedDel($id);
//    }
    public function changeStatus(Request $request)
    {
        $id = $request->get("id");
        // 需要修改两个地方 一个是状态 一个 app是否上架
        $status = (int)$request->get('status');
        return $this->repoist->unifiedUpdateStatus($id, $status);
    }
}
